#include "interface.h"
#include "SD_Manage_Itrfc.h"
#include "Inter_Login_Itrfc.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>

/* 函数功能：点击按钮“上传“的动作
 * 入口参数：
 * 出口参数：无
 * 返 回 值 ：int
 *         SUCCESS  函数成功执行
 *         FAILURE  函数执行出错
 * 创建日期：2009年7月8号
 * 创 建 人 ：匡波 马小艳
 * 更新历史：暂无
 */
gint on_Upload_clicked(GtkWidget *widegt, GdkEvent *event, gpointer data)
{
	/* 设置flag，避免进度条的使用冲突*/
	if (flag)
    {
        showDialog(NULL,
                  GTK_DIALOG_DESTROY_WITH_PARENT,
                  GTK_MESSAGE_ERROR,
                  GTK_BUTTONS_CLOSE,
                  "正在上传或下载文件，请稍后!" );
        
        return FAILURE;
    }
    else
    {
        flag = TRUE;
    }

    /* 树相关变量 */
    GtkTreeView *treeView = NULL;
    GtkTreeModel *model = NULL;
    GtkTreeIter iter;
    GtkTreeIter piter;
    GtkTreeIter citer;
    GtkTreeSelection *select = NULL;
    
    /* 文件相关变量 */
    gchar *filename = NULL;
    gchar path[MAXPATH];
    gchar temppath[MAXPATH];
    gchar *pc = NULL; 
    
    /* 获取窗口的treeview 控件 */
    treeView = GTK_TREE_VIEW( glade_xml_get_widget (windowSDManage, "SdLocalTree") );
    model = gtk_tree_view_get_model(treeView);
    select = gtk_tree_view_get_selection (GTK_TREE_VIEW(treeView));

    if (gtk_tree_selection_get_selected(select, &model, &iter))
    {
	     memset(path, sizeof( path ), 0);
         memset(temppath, sizeof( temppath ), 0);

         gtk_tree_model_get(model, &iter, FILE_NAME, &filename,  -1);
         strcpy(path, filename);
         pc = strchr(path, '\n');
         if (pc)
            *pc = '\0';

         /* 选项为文件夹，出错返回 */
         if ( gtk_tree_model_iter_has_child(model, &iter))
         {
               showDialog(NULL,
                          GTK_DIALOG_DESTROY_WITH_PARENT,
                          GTK_MESSAGE_ERROR,
                          GTK_BUTTONS_CLOSE,
                          "文件夹不可上传，请选择文件上传!" );
	   
	          return FAILURE;
         }

         /* 获取父节点的值 */
         citer = iter;
         while(gtk_tree_model_iter_parent( model, &piter, &citer))
         {
               /* 父节点的字符串值 */
               gtk_tree_model_get(model, &piter, FILE_NAME, &filename,  -1);
               strcpy(temppath, filename);
               
               /* 去掉末尾的'\n'字符 */
               pc = strchr(temppath, '\n');
               if ( pc )
                  *pc = '\0';
               
               /* 拼接成文件路径 */
               strcat(temppath, path);
               strcpy(path, temppath);

#ifdef DEBUG
               g_print("temppath = %s, path = %s", filename, path);
#endif
               /* 查找父节点的父节点 */
               citer = piter;
         }
               
               /* TODO 根据获取的本地文件路径，上传文件 */
               g_print( "path = %s\n", path);
    }
    else 
    {
       showDialog(NULL,
                  GTK_DIALOG_DESTROY_WITH_PARENT,
                  GTK_MESSAGE_ERROR,
                  GTK_BUTTONS_CLOSE,
                  "请选择文件上传!" );
	   
	   return FAILURE;
    } 

    /* 设置进度条 */ 
    pbar = glade_xml_get_widget(windowSDManage, "progressbar1");
        
    gtk_progress_bar_set_orientation(GTK_PROGRESS_BAR (pbar), GTK_PROGRESS_LEFT_TO_RIGHT);
   
    if (ACTIVITY_MODE) 
        gtk_progress_bar_pulse (GTK_PROGRESS_BAR (pbar));

    Pb.step = 0.01;
    Pb.pb = GTK_PROGRESS_BAR (pbar); 

    gtk_timeout_add(100, progress_timeout, &Pb); 
	
    return SUCCESS;
}

/* 函数功能：点击按钮“下载“的动作
 * 入口参数：
 * 出口参数：无
 * 返 回 值 ：int
 *         SUCCESS  函数成功执行
 *         FAILURE  函数执行出错
 * 创建日期：2009年7月8号
 * 创 建 人 ：匡波 马小艳
 * 更新历史：暂无
 */
gint on_Download_clicked(GtkWidget *widegt, gpointer data)
{
    /* 设置flag，避免进度条的使用冲突*/
    if (flag)
    {
        showDialog(NULL,
                  GTK_DIALOG_DESTROY_WITH_PARENT,
                  GTK_MESSAGE_ERROR,
                  GTK_BUTTONS_CLOSE,
                  "正在上传或下载文件，请稍后!" );
        return FAILURE;
    }
    else
    {
        flag = TRUE;
    }
    
    /* 树相关变量 */
    GtkTreeView *treeView;
    GtkTreeModel *model;
    GtkTreeIter iter;
    GtkTreeIter piter;
    GtkTreeIter citer;
    GtkTreeSelection *select = NULL;
    
    /* 文件相关变量 */
    gchar *filename = NULL;
    gchar path[MAXPATH];
    gchar temppath[MAXPATH];
    gchar *pc = NULL; 

    int argc = 0;
    char ** argv = NULL;
    
    /* 获取窗口的treeview控件 */
    treeView = GTK_TREE_VIEW( glade_xml_get_widget (windowSDManage, "SdServerTree") );
    model = gtk_tree_view_get_model(treeView);
    select = gtk_tree_view_get_selection (GTK_TREE_VIEW(treeView));

    if (gtk_tree_selection_get_selected(select, &model, &iter))
    {
	 
         memset(path, sizeof( path ), 0);
         memset(temppath, sizeof( temppath ), 0);

         gtk_tree_model_get(model, &iter, FILE_NAME, &filename,  -1);
         strcpy(path, filename);
         pc = strchr(path, '\n');
         if (pc)
            *pc = '\0';

         /* 选项为文件夹，出错返回 */
         if ( gtk_tree_model_iter_has_child(model, &iter))
         {
               showDialog(NULL,
                          GTK_DIALOG_DESTROY_WITH_PARENT,
                          GTK_MESSAGE_ERROR,
                          GTK_BUTTONS_CLOSE,
                          "文件夹不可下载，请选择文件下载!" );
	   
	          return FAILURE;
         }

         /* 获取父节点的值 */
         citer = iter;
         while(gtk_tree_model_iter_parent( model, &piter, &citer))
         {
               /* 父节点的字符串值 */
               gtk_tree_model_get(model, &piter, FILE_NAME, &filename,  -1);
               strcpy(temppath, filename);
               
               /* 去掉末尾的'\n'字符 */
               pc = strchr(temppath, '\n');
               if ( pc )
                  *pc = '\0';
               
               /* 拼接成文件路径 */
               strcat(temppath, path);
               strcpy(path, temppath);

#ifdef DEBUG
               g_print("temppath = %s, path = %s", filename, path);
#endif
               /* 查找父节点的父节点 */
               citer = piter;
         }
               
               /* TODO 根据获取的服务器文件路径，下载文件 */
               g_print( "path = %s\n", path);
    }
    else 
    {
       showDialog(NULL,
                  GTK_DIALOG_DESTROY_WITH_PARENT,
                  GTK_MESSAGE_ERROR,
                  GTK_BUTTONS_CLOSE,
                  "请选择文件下载!" );
	   
	   return FAILURE;
    }
	
    /* 设置进度条 */   
    pbar = glade_xml_get_widget(windowSDManage, "progressbar1");
        
    gtk_progress_bar_set_orientation(GTK_PROGRESS_BAR (pbar), GTK_PROGRESS_LEFT_TO_RIGHT);

    Pb.step = 0.1;
    Pb.pb = GTK_PROGRESS_BAR (pbar);

    if (ACTIVITY_MODE) 
        gtk_progress_bar_pulse (GTK_PROGRESS_BAR (pbar));
     
    gtk_timeout_add(100, progress_timeout, &Pb); 
    
    /* 弹出内部登录系统界面 */
    call(&windowinterlogin, argc, argv, "08 Inter_Login_Itrfc.glade", 0); 

#ifdef DEBUG
    g_print("Get out of on_DownLoad_clicked\n");
#endif

    return SUCCESS;
}

/* 函数功能：点击按钮“退出“的动作
 * 入口参数：
 * 出口参数：无
 * 返 回 值 ：int
 *         SUCCESS  函数成功执行
 *         FAILURE  函数执行出错
 * 创建日期：2009年7月8号
 * 创 建 人 ：匡波 马小艳
 * 更新历史：暂无
 */
gint on_ManageExit_clicked(GtkWidget *widegt, gpointer data)
{
	/* 销毁窗口 */
   GtkWidget *win = NULL;
   
   win = glade_xml_get_widget( windowSDManage, "window1" );
   gtk_widget_destroy( win );

   gtk_main_quit();
   
   return SUCCESS;
}
